package org.example.bootvue.service.impl;

import org.example.bootvue.entity.User;
import org.example.bootvue.mapper.UserMapper;
import org.example.bootvue.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.bootvue.utils.Md5Util;
import org.example.bootvue.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author YourName
 * @since 2025-07-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User findByUsername(String username) {
        return baseMapper.findByUsername(username);
    }

    @Override
    public void add(String username, String password) {
        String md5string= Md5Util.getMD5String(password);
        baseMapper.add(username,md5string);
        return;
    }

    @Override
    public void updateAvatar(String avatarurl) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarurl,id);
    }

    @Override
    public void updatePwd(String newpwd) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newpwd),id);
    }


}
